<?php

require_once 'Zps/View/Factory/Interface.php';

require_once 'Zps/View.php';

/**
 * Haven't bothered making it a Singleton for now.
 */
class Zps_View_Factory implements Zps_View_Factory_Interface
{

    protected $_config = null;

    public function __construct(Zend_Config $config)
    {
        $this->_config = $config;
    }

    public function createInstance($module = null, Zps_View $view = null, array $params = null)
    {
        if (isset($module) && !ctype_alnum($module)) {
            require_once 'Zps/View/Exception.php';
            throw new Zps_View_Exception('Invalid module name; must only contain alphanumeric characters');
        }
        if (isset($view) && is_null($module)) {
            $subView = clone $view;
        } else {
            if (is_null($module)) {
                $module = 'default'; // assume the default module
            }
            $subView = new Zps_View();
            /**
             * This is the conventional ZF directory layout. Some configuration
             * improvements could allow more flexibility here.
             */
            $subView->setBasePath(
                Zend_Registry::get('BasePath') . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'views'
            );
            if(isset($view)) {
                $subView->setMainView($view->getMainView());
            }
            $subView->setEncoding($this->_config->encoding);
            $subView->setEscape($this->_config->escape);
        }
        /**
         * Place holder for parameters set by a parent view on a sub-view which
         * can be used as inputs to certain View Helpers in the sub view.
         */
        if (isset($params)) {
            $subView->params = $params;
        }
        return $subView;
    }

}